package com.xzq.wanandroid.base.presenter;

import android.view.View;

import java.lang.ref.WeakReference;

import io.reactivex.disposables.CompositeDisposable;

/**
 * @ClassName : BasePresenter
 * @Description : 基础的P类
 * @Author : xzq
 * @Date: 2021-05-08 16:24
 * @Version :
 */
public class BasePresenter<V>{

    /**
     * 对订阅请求统一管理
     */
    protected CompositeDisposable compositeDisposable = new CompositeDisposable();
    private WeakReference<V> weakReference;

    /**
     * 创建弱引用View
     */
    public void attchView(V view) {
        weakReference = new WeakReference<V>(view);
    }

    /**
     * 将View取出
     */

    protected V getView() {
        return weakReference == null ? null : weakReference.get();
    }

    /**
     * 是否使用弱引用创建View
     */

    protected boolean isViewAttached() {
        return weakReference != null && weakReference.get() != null;
    }

    /**
     * 释放View并取消订阅
     */

    public void detechView() {
        if (weakReference != null) {
            weakReference.clear();
            weakReference = null;
        }
        compositeDisposable.clear();
    }

}


